/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                  *
 *        *************************************************         *
 *        *©2021-2027 HT Corporation All rights reserved *          *
 *        *************************************************         *
 *                                                                  *
 * FileName    : h_vpss.c                                           *
 *                                                                  *
 * Author      : linus                                              *
 *                                                                  *
 * Email       : luoyaojun@haitutech.com                            *
 *                                                                  *
 * Date        : 2022-8-24                                          *
 *                                                                  *
 * Description :                                                    *
 *                                                                  *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "h_vpss.h"
#include "ht_vpss.h"

static HT_S32 ht_vpss_group_set_abs_crop(HT_S32 vpss_grp, HT_U32 enable, struct rect *prect)
{
	struct ht_vpss *pvpss = HT_MPP_HANDLE_VPSS;
	HT_S32 ret = -1;
	
	if (pvpss && pvpss->rt_config && pvpss->rt_config->vpss_grp_rt_set_abs_crop)
		ret = pvpss->rt_config->vpss_grp_rt_set_abs_crop(pvpss, vpss_grp, enable, prect);

	return ret;
}

HT_S32 ht_vpss_group_init(HT_U32 vpss_grp, HT_PIXEL_FORMAT src_pixel_format)
{
    struct ht_vpss *pvpss = HT_MPP_HANDLE_VPSS;
    HT_S32 ret = -1;

    if (!pvpss || (src_pixel_format > HT_FMT_BUTT))
    {
        printf("VPSS CORE init faild! pvpss = %p, src_pixel_format = %d (HT_FMT_BUTT = %d)\n", pvpss, src_pixel_format,
               HT_FMT_BUTT);
        return -1;
    }

    if (!pvpss->ops)
        return -2;

    if (pvpss->ops->vpss_grp_init)
        ret = pvpss->ops->vpss_grp_init(pvpss, vpss_grp, src_pixel_format);

    return ret;
}

HT_S32 ht_vpss_chn_get_frame(HT_S32 vpss_grp, HT_S32 vpss_chn, ht_vpss_chn_get_frame_cb cb, void *user)
{
	struct ht_vpss *pvpss = HT_MPP_HANDLE_VPSS;
	HT_S32 ret = -1;

    if (!pvpss->ops)
        return -2;

    if (pvpss->ops->vpss_chn_get_frame)
        ret = pvpss->ops->vpss_chn_get_frame(pvpss, vpss_grp, vpss_chn, cb, user);	

	return ret;
}

HT_S32 ht_vpss_chn_get_info(HT_S32 vpss_grp, HT_S32 vpss_chn, HT_S32 *fd, HT_S32 *width, HT_S32 *height)
{
	struct ht_vpss *pvpss = HT_MPP_HANDLE_VPSS;
	HT_S32 ret = -1;

    if (!pvpss->ops)
        return -2;

    if (pvpss->ops->vpss_chn_get_info)
        ret = pvpss->ops->vpss_chn_get_info(pvpss, vpss_grp, vpss_chn, fd, width, height);	

	return ret;

}

HT_S32 ht_vpss_group_enable_abs_crop(HT_S32 vpss_grp, struct rect *prect)
{
	return (ht_vpss_group_set_abs_crop(vpss_grp, 1, prect));
}

HT_S32 ht_vpss_group_disable_abs_crop(HT_S32 vpss_grp)
{
	return (ht_vpss_group_set_abs_crop(vpss_grp, 0, NULL));
}

HT_S32 ht_vpss_exit(HT_U32 vpss_grp)
{
    struct ht_vpss *pvpss = HT_MPP_HANDLE_VPSS;
    HT_S32 ret = -1;

    if (!pvpss->ops)
        return -1;

    if (pvpss->ops->vpss_grp_exit)
        ret = pvpss->ops->vpss_grp_exit(pvpss, vpss_grp);

    return ret;
}

HT_S32 ht_vpss_chn_init(HT_U32 vpss_grp, HT_U32 vpss_chn, HT_U32 width, HT_U32 height, HT_PIXEL_FORMAT fmt,
                        HT_U32 u32Depth)
{
    struct ht_vpss *pvps = HT_MPP_HANDLE_VPSS;
    HT_S32 ret = -1;
    struct resolution res = {.width = width, .height = height};

    if (!pvps->ops)
        return -1;

    if (pvps->ops->vpss_chn_init)
        ret = pvps->ops->vpss_chn_init(pvps, vpss_grp, vpss_chn, &res, fmt, u32Depth);

    return ret;
}

HT_S32 ht_vpss_chn_exit(HT_U32 vpss_grp, HT_U32 vpss_chn)
{
	struct ht_vpss *pvpss = HT_MPP_HANDLE_VPSS;
	HT_S32 ret = -1;
	
	if (pvpss && pvpss->ops && pvpss->ops->vpss_chn_exit)
		ret = pvpss->ops->vpss_chn_exit(pvpss, vpss_grp, vpss_chn);

	return ret;
}

HT_S32 ht_vpss_module_register(struct ht_vpss *pvpss)
{
    return (ht_register_modules(pvpss->m_id, (HT_HANDLE)pvpss));
}

